<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
            
        }
        .box{
            width: 500px;
            height: 350px;
            margin: 0 auto;
            position: relative;
            /* border: 1px solid #000; */
        }
        span{
            position: absolute;
            width: 40px;
            height: 80px;
            line-height: 80px;
            text-align: center;
            font-size: 40px;
            background-image: linear-gradient(rgba(0, 0, 0, 0.3),rgba(0, 0, 0, 0.3));
            color: aqua;
            top: calc(50% - 40px);
            opacity: 0;
            transition: opacity .3s linear;
        }
        .prev{
            left: 0;
        }
        .next{
            right: 0;
        }
        .box:hover span{
            opacity: 1;
        }
        .prev:hover{
            background-image: linear-gradient(90deg,rgba(255, 255, 255, 0.741),rgba(255, 255, 255, 0.027));
        }
        .next:hover{
            background-image: linear-gradient(-90deg,rgba(255, 255, 255, 0.741),rgba(255, 255, 255, 0.027));
        }

        ul{
            display: flex;
            position: relative;
            left: 0;
            transition: left .3s linear;
        }
        ul > li{
            width: 500px;
            height: 350px;
        }
        ul > li img{
            display: block;
            width: 100%;
            height: 100%;
        }
        ol{
            position: absolute;
            display: flex;
            bottom: 20px;
            width: 100%;
            justify-content: center;
        }
        ol > li{
            width: 10px;
            height: 10px;
            border-radius: 10px;
            margin: 0 8px;
            background-color: white;
        }
        ol > li.action{
            background-color: rgb(0, 0, 0);
        }
    </style>
    
</head>
<body>
    <div class="box">
        <ul>
           
        </ul>
        <span class="prev">&lt;</span>
        <span class="next">&gt;</span>
        <ol>
            
        </ol>
    </div>
    <script>
        var image = ["https://www.jq22.com/demo/jquerylbt202112220954/images/t1.png",
        "https://www.jq22.com/demo/jquerylbt202112220954/images/t2.png",
        "https://www.jq22.com/demo/jquerylbt202112220954/images/t3.png"];

        // 1.根据image的长度 创建ul中的li 与ol中的li
        var str = "",str1 = "";
        for(var i = 0;i < image.length;i++){
            str += `<li><img src="${image[i]}" alt=""></li>`
            str1 += `<li></li>`
        }
        console.log(str);
        console.log(str1);
        var box = document.querySelector(".box");
        var ul = document.querySelector(".box > ul");
        var ol = document.querySelector(".box > ol");
        ul.innerHTML = str;
        ol.innerHTML = str1;
        console.log(ul.children[0].offsetWidth);
        ul.style.width = image.length *  box.offsetWidth + "px";
        ol.children[0].classList.add("action");


        // .点击span.next设置显示下一个li标签
        // .点击span.prev设置显示上一个li标签
        // 点击小圆点设置显示相对应索引值的li标签
        // 每隔一段时间就自动显示下一个li标签

        var next = document.querySelector(".next");
        var prev = document.querySelector(".prev");
        var ollis = document.querySelectorAll("ol > li");

        // 当前显示的是li0
        var i = 0;
        next.onclick = b;
        
        
        function b(){
            // 在点击显示下一张,记录要显示的li的索引值
            i++;
            if(i > image.length - 1) i = 0;
            // console.log(i);
            a()
        }

        setInterval(b,2000)

        prev.onclick = function(){
            i--;
            if(i < 0) i = image.length - 1;
            // console.log(i);
            a()
        }

        function a(){
            var l = i * -box.offsetWidth + "px";
            ul.style.left = l;

            for(var k = 0;k < ollis.length;k++){
                ollis[k].classList.remove("action");
            }
            ollis[i].classList.add("action");
        }


        for(let m = 0;m < ollis.length;m++){
            ollis[m].onclick = function(){
                // m 当前点击li的索引值
                i = m;
                a()
            }
        }


    </script>
</body>
</html>